home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / RGXPRIM.C < prev    next >
C/C++ Source or Header  |  1991-04-02  |  9KB  |  236 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/microcode/RCS/rgxprim.c,v 1.9 1991/04/02 19:45:24 cph Exp $
  4.  
  5. Copyright (c) 1987-91 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* Primitives for regular expression matching and search. */
  36.  
  37. #include "scheme.h"
  38. #include "prims.h"
  39. #include "edwin.h"
  40. #include "syntax.h"
  41. #include "regex.h"
  42.  
  43. #define RE_CHAR_SET_P(object)                        \
  44.   ((STRING_P (object)) &&                        \
  45.    ((STRING_LENGTH (object)) == (MAX_ASCII / ASCII_LENGTH)))
  46.  
  47. #define CHAR_SET_P(argument)                        \
  48.   ((STRING_P (argument)) && ((STRING_LENGTH (argument)) == MAX_ASCII))
  49.  
  50. #define CHAR_TRANSLATION_P(argument)                    \
  51.   ((STRING_P (argument)) && ((STRING_LENGTH (argument)) == MAX_ASCII))
  52.  
  53. #define RE_REGISTERS_P(object)                        \
  54.   (((object) == SHARP_F) ||                        \
  55.    ((VECTOR_P (object)) &&                        \
  56.     ((VECTOR_LENGTH (object)) == (RE_NREGS + RE_NREGS))))
  57.  
  58. #define RE_MATCH_RESULTS(result, vector) do                \
  59. {                                    \
  60.   if ((result) >= 0)                            \
  61.     {                                    \
  62.       if ((vector) != SHARP_F)                        \
  63.     {                                \
  64.       int i;                            \
  65.       long index;                            \
  66.                                     \
  67.       for (i = 0; (i < RE_NREGS); i += 1)                \
  68.         {                                \
  69.           index = ((registers . start) [i]);            \
  70.           VECTOR_SET                        \
  71.         (vector,                        \
  72.          i,                            \
  73.          ((index == -1)                        \
  74.           ? SHARP_F                        \
  75.           : (long_to_integer (index))));            \
  76.           index = ((registers . end) [i]);                \
  77.           VECTOR_SET                        \
  78.         (vector,                        \
  79.          (i + RE_NREGS),                    \
  80.          ((index == -1)                        \
  81.           ? SHARP_F                        \
  82.           : (long_to_integer (index))));            \
  83.         }                                \
  84.     }                                \
  85.       PRIMITIVE_RETURN (long_to_integer (result));            \
  86.     }                                    \
  87.   else if ((result) == (-1))                        \
  88.     PRIMITIVE_RETURN (SHARP_F);                        \
  89.   else if ((result) == (-2))                        \
  90.     error_bad_range_arg (1);                        \
  91.   else                                    \
  92.     error_external_return ();                        \
  93. } while (0)
  94.  
  95. DEFINE_PRIMITIVE ("RE-CHAR-SET-ADJOIN!", Prim_re_char_set_adjoin, 2, 2, 0)
  96. {
  97.   int ascii;
  98.   PRIMITIVE_HEADER (2);
  99.   CHECK_ARG (1, RE_CHAR_SET_P);
  100.   ascii = (arg_ascii_integer (2));
  101.   (* (STRING_LOC ((ARG_REF (1)), (ascii / ASCII_LENGTH)))) |=
  102.     (1 << (ascii % ASCII_LENGTH));
  103.   PRIMITIVE_RETURN (UNSPECIFIC);
  104. }
  105.  
  106. DEFINE_PRIMITIVE ("RE-COMPILE-FASTMAP", Prim_re_compile_fastmap, 4, 4, 0)
  107. {
  108.   fast SCHEME_OBJECT pattern;
  109.   fast int can_be_null;
  110.   PRIMITIVE_HEADER (4);
  111.   CHECK_ARG (1, STRING_P);
  112.   pattern = (ARG_REF (1));
  113.   CHECK_ARG (2, CHAR_TRANSLATION_P);
  114.   CHECK_ARG (3, SYNTAX_TABLE_P);
  115.   CHECK_ARG (4, CHAR_SET_P);
  116.   can_be_null =
  117.     (re_compile_fastmap
  118.      ((STRING_LOC (pattern, 0)),
  119.       (STRING_LOC (pattern, (STRING_LENGTH (pattern)))),
  120.       (STRING_LOC ((ARG_REF (2)), 0)),
  121.       (ARG_REF (3)),
  122.       (STRING_LOC ((ARG_REF (4)), 0))));
  123.   if (can_be_null >= 0)
  124.     PRIMITIVE_RETURN (long_to_integer (can_be_null));
  125.   else if (can_be_null == (-2))
  126.     error_bad_range_arg (1);
  127.   else
  128.     error_external_return ();
  129. }
  130.  
  131. /* (re-match-substring regexp translation syntax-table registers
  132.                string start end)
  133.  
  134.    Attempt to match REGEXP against the substring [STRING, START, END].
  135.    Return the index of the end of the match (exclusive) if successful.
  136.    Otherwise return false.  REGISTERS, if not false, is set to contain
  137.    the appropriate indices for the match registers. */
  138.  
  139. #define RE_SUBSTRING_PRIMITIVE(procedure)                \
  140. {                                    \
  141.   fast SCHEME_OBJECT regexp;                        \
  142.   long match_start, match_end, text_end;                \
  143.   unsigned char * text;                            \
  144.   struct re_buffer buffer;                        \
  145.   struct re_registers registers;                    \
  146.   int result;                                \
  147.   PRIMITIVE_HEADER (7);                            \
  148.   CHECK_ARG (1, STRING_P);                        \
  149.   regexp = (ARG_REF (1));                        \
  150.   CHECK_ARG (2, CHAR_TRANSLATION_P);                    \
  151.   CHECK_ARG (3, SYNTAX_TABLE_P);                    \
  152.   CHECK_ARG (4, RE_REGISTERS_P);                    \
  153.   CHECK_ARG (5, STRING_P);                        \
  154.   match_start = (arg_nonnegative_integer (6));                \
  155.   match_end = (arg_nonnegative_integer (7));                \
  156.   text = (STRING_LOC ((ARG_REF (5)), 0));                \
  157.   text_end = (STRING_LENGTH (ARG_REF (5)));                \
  158.   if (match_end > text_end) error_bad_range_arg (7);            \
  159.   if (match_start > match_end) error_bad_range_arg (6);            \
  160.   re_buffer_initialize                            \
  161.     ((& buffer), (STRING_LOC ((ARG_REF (2)), 0)), (ARG_REF (3)),    \
  162.      text, 0, text_end, text_end, text_end);                \
  163.   result =                                \
  164.     (procedure ((STRING_LOC (regexp, 0)),                \
  165.         (STRING_LOC (regexp, (STRING_LENGTH (regexp)))),    \
  166.         (& buffer),                        \
  167.         (((ARG_REF (4)) == SHARP_F) ? NULL : (& registers)),    \
  168.         (& (text [match_start])),                \
  169.         (& (text [match_end]))));                \
  170.   RE_MATCH_RESULTS (result, (ARG_REF (4)));                \
  171. }
  172.  
  173. DEFINE_PRIMITIVE ("RE-MATCH-SUBSTRING", Prim_re_match_substring, 7, 7, 0)
  174.      RE_SUBSTRING_PRIMITIVE (re_match)
  175.  
  176. DEFINE_PRIMITIVE ("RE-SEARCH-SUBSTRING-FORWARD", Prim_re_search_substr_forward, 7, 7, 0)
  177.      RE_SUBSTRING_PRIMITIVE (re_search_forward)
  178.  
  179. DEFINE_PRIMITIVE ("RE-SEARCH-SUBSTRING-BACKWARD", Prim_re_search_substr_backward, 7, 7, 0)
  180.      RE_SUBSTRING_PRIMITIVE (re_search_backward)
  181.  
  182. #define RE_BUFFER_PRIMITIVE(procedure)                    \
  183. {                                    \
  184.   fast SCHEME_OBJECT regexp, group;                    \
  185.   long match_start, match_end, text_start, text_end, gap_start;        \
  186.   unsigned char * text;                            \
  187.   struct re_buffer buffer;                        \
  188.   struct re_registers registers;                    \
  189.   int result;                                \
  190.   PRIMITIVE_HEADER (7);                            \
  191.   CHECK_ARG (1, STRING_P);                        \
  192.   regexp = (ARG_REF (1));                        \
  193.   CHECK_ARG (2, CHAR_TRANSLATION_P);                    \
  194.   CHECK_ARG (3, SYNTAX_TABLE_P);                    \
  195.   CHECK_ARG (4, RE_REGISTERS_P);                    \
  196.   CHECK_ARG (5, GROUP_P);                        \
  197.   group = (ARG_REF (5));                        \
  198.   match_start = (arg_nonnegative_integer (6));                \
  199.   match_end = (arg_nonnegative_integer (7));                \
  200.   text = (STRING_LOC ((GROUP_TEXT (group)), 0));            \
  201.   text_start = (MARK_INDEX (GROUP_START_MARK (group)));            \
  202.   text_end = (MARK_INDEX (GROUP_END_MARK (group)));            \
  203.   gap_start = (GROUP_GAP_START (group));                \
  204.   if (text_end > gap_start)                        \
  205.     text_end += (GROUP_GAP_LENGTH (group));                \
  206.   if (match_end > gap_start)                        \
  207.     {                                    \
  208.       match_end += (GROUP_GAP_LENGTH (group));                \
  209.       if (match_start >= gap_start)                    \
  210.     match_start += (GROUP_GAP_LENGTH (group));            \
  211.     }                                    \
  212.   if (match_start > match_end) error_bad_range_arg (6);            \
  213.   if (match_end > text_end) error_bad_range_arg (7);            \
  214.   if (match_start < text_start) error_bad_range_arg (6);        \
  215.   re_buffer_initialize                            \
  216.     ((& buffer), (STRING_LOC ((ARG_REF (2)), 0)), (ARG_REF (3)),    \
  217.      text, text_start, text_end, gap_start, (GROUP_GAP_END (group)));    \
  218.   result =                                \
  219.     (procedure ((STRING_LOC (regexp, 0)),                \
  220.         (STRING_LOC (regexp, (STRING_LENGTH (regexp)))),    \
  221.         (& buffer),                        \
  222.         (((ARG_REF (4)) == SHARP_F) ? NULL : (& registers)),    \
  223.         (& (text [match_start])),                \
  224.         (& (text [match_end]))));                \
  225.   RE_MATCH_RESULTS (result, (ARG_REF (4)));                \
  226. }
  227.  
  228. DEFINE_PRIMITIVE ("RE-MATCH-BUFFER", Prim_re_match_buffer, 7, 7, 0)
  229.      RE_BUFFER_PRIMITIVE (re_match)
  230.  
  231. DEFINE_PRIMITIVE ("RE-SEARCH-BUFFER-FORWARD", Prim_re_search_buffer_forward, 7, 7, 0)
  232.      RE_BUFFER_PRIMITIVE (re_search_forward)
  233.  
  234. DEFINE_PRIMITIVE ("RE-SEARCH-BUFFER-BACKWARD", Prim_re_search_buffer_backward, 7, 7, 0)
  235.      RE_BUFFER_PRIMITIVE (re_search_backward)
  236.